package cy;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.List;

public class Dom4JDemo {
    public static void main(String[] args) throws DocumentException {
        //1。利用SaxReader读取一个xml文件
        Document document = new SAXReader().read("D:\\cy-javaEE-project\\day01-xml-json\\src\\Demo02.xml");
//        System.out.println(document);
        //利用Document获取根节点
        Element rootElement = document.getRootElement();
        System.out.println(rootElement.getName());
        System.out.println("-------------------------");
        //获取根节点下的子节点
        List<Element> elements = rootElement.elements();
        System.out.println(elements.size());
        System.out.println("--------------------------");
        for (Element element : elements) {
            System.out.println(element.getName()+" "+
                    element.attributeValue("pid")+" "+
                    element.attributeValue("color"));
            for (Element ele : (List<Element>) element.elements()) {
                System.out.println(ele.getName()+" "+
                        ele.getText()+" "+
                        ele.attributeValue("name"));
            }
        }
    }
}
